package com.ac.zhly.utils;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class GenerateRSAKeyPair {
    public GenerateRSAKeyPair() throws NoSuchAlgorithmException {
    }

    public static KeyPair generateRSAKeyPair(int keySize) throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(keySize);
        return keyPairGen.generateKeyPair();
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        // 示例：生成 2048 位密钥对（推荐安全长度）
        KeyPair keyPair = generateRSAKeyPair(2048);
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        System.out.println("公钥: " + publicKey.getModulus().toString());
        System.out.println("私钥: " + privateKey.getModulus().toString());
    }

}
